package com.treasure.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.treasure.common.exception.ParameterException;
import com.treasure.common.exception.ServiceException;
import com.treasure.common.model.SessionUser;
import com.treasure.common.model.SessionUserHolder;
import com.treasure.common.types.Corp;
import com.treasure.common.types.CorpInvitationCode;
import com.treasure.infrastructure.model.po.CorpUserInfo;
import com.treasure.infrastructure.repository.dao.mapper.CorpUserInfoMapper;
import com.treasure.web.command.request.CorpInfoEditRequest;
import com.treasure.web.command.response.CorpInfoResponse;
import com.treasure.web.mapstruct.CorpInfoAssembler;
import com.treasure.web.service.ICorpInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.treasure.infrastructure.repository.dao.mapper.CorpInfoMapper;
import com.treasure.infrastructure.model.po.CorpInfo;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sunhongmin
 * @since 2023-11-11
 */
@Service
@RequiredArgsConstructor
public class CorpInfoServiceImpl extends ServiceImpl<CorpInfoMapper, CorpInfo> implements ICorpInfoService {
    private final CorpUserInfoMapper corpUserInfoMapper;

    @Override
    @NotNull
    public CorpInfo getEffectiveCorpInfo(Corp corp) {
        QueryWrapper<CorpInfo> corpInfoQuery = new QueryWrapper<>();
        corpInfoQuery.lambda().eq(CorpInfo::getCorpId, corp.getValue())
                .gt(CorpInfo::getExpiryDate, LocalDateTime.now()) // 校验有效期
        ;
        CorpInfo corpInfo = getOne(corpInfoQuery);
        if (Objects.isNull(corpInfo)) {
            throw new ServiceException("所属企业/高校已到期，corpId=" + corp.getValue());
        }
        return corpInfo;
    }

    @Override
    public CorpInfo getEffectiveCorpInfo(CorpInvitationCode corpInvitationCode) {
        QueryWrapper<CorpInfo> corpInfoQuery = new QueryWrapper<>();
        corpInfoQuery.lambda().eq(CorpInfo::getInviteCode, corpInvitationCode.getValue())
        ;
        CorpInfo corpInfo = getOne(corpInfoQuery);
        if (Objects.isNull(corpInfo)) {
            throw new ServiceException("未获取到主体信息，请确认认证码是否正确，invitationCode=" + corpInvitationCode.getValue());
        }
        // 校验有效期
        if (corpInfo.getExpiryDate().isBefore(LocalDate.now())) {
            throw new ServiceException("企业/高校已到期，corpId=" + corpInfo.getCorpId());
        }
        return corpInfo;
    }

    @Override
    public CorpInfoResponse getCorpInfo() {
        SessionUser sessionUser = SessionUserHolder.getAndCheckSessionUser();
        QueryWrapper<CorpInfo> query = new QueryWrapper<>();
        query.lambda().eq(CorpInfo::getCorpId, sessionUser.getCorpId());
        CorpInfo corpInfo = getOne(query);
        return CorpInfoAssembler.INSTANCE.toCorpInfoResponse(corpInfo);
    }

    @Override
    public void corpInfoEdit(CorpInfoEditRequest corpInfoEditRequest) {
        SessionUser sessionUser = SessionUserHolder.getAndCheckSessionUser();
        QueryWrapper<CorpInfo> query = new QueryWrapper<>();
        query.lambda().eq(CorpInfo::getCorpId, sessionUser.getCorpId());
        CorpInfo corpInfo = getOne(query);
        if (Objects.isNull(corpInfo)) {
            throw new ParameterException("企业信息获取失败, corpId=" + sessionUser.getCorpId());
        }

        CorpInfo updateCorp = new CorpInfo();
        updateCorp.setId(corpInfo.getId());
        updateCorp.setCorpName(corpInfoEditRequest.getCorpName());
        updateCorp.setIndustry(corpInfoEditRequest.getIndustry());
        updateCorp.setLogoUrl(corpInfoEditRequest.getLogoUrl());
        updateCorp.setCustomSiteTitle(corpInfoEditRequest.getCustomSiteTitle());
        updateCorp.setContactor(corpInfoEditRequest.getContactor());
        updateCorp.setPurpose(corpInfoEditRequest.getPurpose());
        updateById(updateCorp);
    }

    @Override
    public List<CorpUserInfo> queryUserListByNameLike(String userName, String corpId) {
        QueryWrapper<CorpUserInfo> userNameLikeQuery = new QueryWrapper<>();
        userNameLikeQuery.lambda()
                .like(CorpUserInfo::getUsername, userName);
        if (StringUtils.isNotBlank(corpId)) {
            userNameLikeQuery.lambda()
                    .eq(CorpUserInfo::getCorpId, corpId);
        }
        return corpUserInfoMapper.selectList(userNameLikeQuery);
    }
}
